package com.liser.southernstar.ui.activity;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.google.android.material.appbar.AppBarLayout;
import com.liser.southernstar.R;
import com.liser.southernstar.framework.ui.BaseActivity;
import com.liser.southernstar.collection.ui.fragment.EmptyFragment;
import com.liser.southernstar.collection.ui.fragment.OrderFragment;
import com.liser.southernstar.eleme.util.DensityUtil;
import com.liser.southernstar.eleme.view.behavior.AppBarLayoutScrollBehavior;
import com.liser.southernstar.eleme.view.widget.indicator.Indicator;
import com.liser.southernstar.eleme.view.widget.indicator.IndicatorViewPager;
import com.liser.southernstar.eleme.view.widget.indicator.OnTransitionTextListener;
import com.liser.southernstar.eleme.view.widget.indicator.slidebar.ColorBar;
import com.liser.southernstar.eleme.view.widget.nested.NestedViewPager;

/**
 * @author LISER
 */
public class ElemeMainActivity extends BaseActivity implements AppBarLayoutScrollBehavior.OnAppBarListener {

    /**
     * "点餐",
     * "评论",
     * "商家"
     */
    private final String[] TITLES = {
            "点餐",
            "商家"
    };

    private final int DEFAULT_SELECTED_TAB = 0;//默认选中

    private final int PAGE_COUNT = TITLES.length;

    private OrderFragment mOrderFragment;

    private Fragment mCommentFragment;

    private Fragment mShopDetailFragment;

    private NestedViewPager mViewPager;

    @Override
    protected int onLayout() {
        return R.layout.activity_main_eleme;
    }

    @Override
    protected void onView() {
        initAppBar();
        initViewPager();
    }

    private void initAppBar() {
        AppBarLayout mAppBarLayout = findViewById(R.id.appbar_layout);
        AppBarLayoutScrollBehavior mAppBarLayoutBehavior = new AppBarLayoutScrollBehavior(this, this);
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
        params.setBehavior(mAppBarLayoutBehavior);
        mAppBarLayout.setLayoutParams(params);
    }

    private void initViewPager() {
        mViewPager = findViewById(R.id.viewpager);
        Indicator indicator = findViewById(R.id.sliding_tabs);

        final int selectColor = ContextCompat.getColor(this, R.color.home_blue);
        int unSelectColor = ContextCompat.getColor(this, R.color.home_black);
        indicator.setOnTransitionListener(new OnTransitionTextListener().setColor(selectColor, unSelectColor));

        mViewPager.setOffscreenPageLimit(PAGE_COUNT);

        IndicatorViewPager mIndicatorViewPager = new IndicatorViewPager(indicator, mViewPager);
        mIndicatorViewPager.setAdapter(new TabAdapter(getSupportFragmentManager()));
        mIndicatorViewPager.setCurrentItem(DEFAULT_SELECTED_TAB, false);

        ColorBar fixedWidthColorBar = new ColorBar(this, R.drawable.bg_scrollbar, DensityUtil.dip2px(this, 2));
        fixedWidthColorBar.setWidth(DensityUtil.dip2px(this, 30));
        indicator.setScrollBar(fixedWidthColorBar);
    }

    @Override
    public void onTitleBarItemClick(int id) {
        Toast.makeText(this, "onTitleBarItemClick isSearchBtn:" + (id == R.id.iv_title_search)
                , Toast.LENGTH_SHORT)
                .show();
    }

    @Override
    public void initEnd(int miniTopHeight) {
        if (mViewPager != null) {
            mViewPager.setViewHeight(miniTopHeight);
        }
        findViewById(R.id.tvLoading).setVisibility(View.GONE);
    }

    private class TabAdapter extends IndicatorViewPager.IndicatorFragmentPagerAdapter {

        public TabAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public int getCount() {
            return PAGE_COUNT;
        }

        @Override
        public View getViewForTab(final int position, View convertView, ViewGroup container, final int currentSelectedIndex) {
            if (convertView == null) {
                convertView = ElemeMainActivity.this.getLayoutInflater().inflate(R.layout.tab_top, container, false);
            }
            TextView textView = convertView.findViewById(R.id.tv_title);
            textView.setText(TITLES[position]);
            textView.setTextColor(getResources().getColor(R.color.home_black));

            return convertView;
        }

        @Override
        public Fragment getFragmentForPage(int position) {
            switch (position) {
                case 0:
                    if (mOrderFragment == null) {
                        mOrderFragment = new OrderFragment();
                    }
                    return mOrderFragment;
                case 1:
                    if (mCommentFragment == null) {
                        mCommentFragment = new EmptyFragment();
                    }
                    return mCommentFragment;
                case 2:
                    if (mShopDetailFragment == null) {
                        mShopDetailFragment = new EmptyFragment();
                    }
                    return mShopDetailFragment;
            }
            if (mOrderFragment == null) {
                mOrderFragment = new OrderFragment();
            }
            return mOrderFragment;
        }
    }
}
